//
//  WelcomeViewController.swift
//  weibo
//
//  Created by 张宏芝 on 15/9/5.
//  Copyright © 2015年 kama. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

//    定义头像的底部约束
    private var iconButtomCons:NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    iconView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatarUrl)
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        
        let height = -(UIScreen.mainScreen().bounds.height + iconButtomCons!.constant)
        iconButtomCons?.constant = height
        
//        头像动画
        textLabel.alpha = 0
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
//            更新自动布局
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
//                再次嵌套动画效果
                UIView.animateWithDuration(0.8, animations: { () -> Void in
//                    在闭包中,使用self
                    self.textLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(HMSwitchRootViewControllerNotification, object: nil)
                })
        }
        
    }
    
    private func setupUI(){
        view.addSubview(backgroundImage)
        view.addSubview(iconView)
        view.addSubview(textLabel)
        
//      背景图片  自动布局(VFL)
        backgroundImage.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":backgroundImage]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":backgroundImage]))
        
//       头像  自动布局(非VFL)
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -200))
//        新增加的约束的最后一个
        iconButtomCons = view.constraints.last
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        
//   头像下文字的自动布局
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))

        
    }
 
 /// 懒加载控件
    private lazy var backgroundImage:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconView:UIImageView = {
       let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        icon.layer.cornerRadius = 45
        icon.layer.masksToBounds = true
        return icon
    }()
    
//    private lazy var textLabel:UILabel = {
//       let label = UILabel()
//        label.text = "欢迎回来"
//        return label
//    }()
    
    
//    private lazy var textLabel:UILabel = UILabel(title: "欢迎回来", color: UIColor.darkGrayColor(), fontSize: 15)
    
    private lazy var textLabel:UILabel = UILabel(title: "欢迎回来", color: UIColor.darkGrayColor(), fontSize: 18, layoutWidth: 300)
    
}
